﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class AddandModificationForm : Form
    {
        private int id;

        public AddandModificationForm()
        {
            InitializeComponent();
        }

        public AddandModificationForm(int id, string title, string author)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var Author = textBox2.Text;
            //更新
            if (id != 0)
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("update Articles set title='{0}',Author={1} where Id= {2}", title, Author, this.id);
                    var res = DbHelper.Operate(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功", "提示");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(Author))
                {
                    var sql = string.Format("insert into Articles(Title,Author) values('{0}','{1}')",title,Author);
                    var res = DbHelper.Operate(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");

                        this.DialogResult = DialogResult.Yes;

                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("标题和作者必须添加", "提示");
                }
            }
        }

        private void AddandModificationForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
